package com.dianrui.firemenmonitor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.dianrui.firemenmonitor.Constant;
import com.dianrui.firemenmonitor.R;
import com.dianrui.firemenmonitor.activity.MainActivity;
import com.dianrui.firemenmonitor.activity.WebViewActivity;
import com.dianrui.firemenmonitor.adapter.MainArticleAdapter;
import com.dianrui.firemenmonitor.bean.MainArticleTop;
import com.dianrui.firemenmonitor.event.StudyFragmentEvent;
import com.dianrui.firemenmonitor.util.OkHttpUtils;
import com.dianrui.firemenmonitor.util.StringUtils;
import com.dianrui.firemenmonitor.util.Url;
import com.dianrui.firemenmonitor.view.PulltoRefresh.PullToRefreshBase;
import com.dianrui.firemenmonitor.view.PulltoRefresh.PullToRefreshListView;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FragStudyList extends BaseFragment {
    private String postId="1";
    private int mPageIndex = 1;
    int type;
    private List<MainArticleTop> mainArticleTopList=new ArrayList<>();
    private MainArticleAdapter mainArticleAdapter;
    @BindView(R.id.listview)
    PullToRefreshListView listView;

    @Override
    public int getContentViewId() {
        return R.layout.fragment_study;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        getArticle(1,false);
        mainArticleAdapter=new MainArticleAdapter(getActivity(),mainArticleTopList);
        listView.setAdapter(mainArticleAdapter);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getArticle(type,true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPageIndex++;
                getArticle(type,false);

            }
        });
        mainArticleAdapter.setOnButtonClickListener(new MainArticleAdapter.MainArticleClickListener() {
            @Override
            public void onMainArticleClick(String htmlurl,String titles) {
                Intent intent=new Intent(getActivity(),WebViewActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("url",htmlurl);
                bundle.putString("titles",titles);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
    }

    @Override
    public void getData() {

    }

    @Subscribe
    public synchronized void onPageSelected(StudyFragmentEvent event) {
        if (event != null) {
            getArticle(event.getType(),true);
             type = event.getType();
        }
    }

    private void getArticle(int postId,final boolean isRfresh) {
        if (isRfresh) {
            mPageIndex = 1;
        }
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("post_id",postId);
        jsonObject.addProperty("p", mPageIndex);
        OkHttpUtils.getInstance().postRequests(Url.CONT, jsonObject.toString(), new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (listView != null) {
                    listView.onRefreshComplete();
                }
                if (isRfresh) {
                    mainArticleTopList.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && isAdded()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("code"))) {
                                if (mainArticleTopList != null) {
                                    mainArticleTopList.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONObject("data").optJSONArray("cont");
                                if (jsonArray.length()>0) {
                                    for (int i = 0; i <jsonArray.length() ; i++) {
                                        MainArticleTop mainArticleTop=new MainArticleTop();
                                        JSONObject jsonObject1 = jsonArray.optJSONObject(i);
                                        mainArticleTop.id= jsonObject1.optString("id");
                                        mainArticleTop.thumbnail= jsonObject1.optString("thumbnail");
                                        mainArticleTop.post_title= jsonObject1.optString("post_title");
                                        mainArticleTop.html_href= jsonObject1.optString("html_href");
                                        mainArticleTop.create_time= jsonObject1.optLong("create_time");
                                        mainArticleTopList.add(mainArticleTop);
                                    }
                                    if (mainArticleAdapter != null) {
                                        mainArticleAdapter.refresh(mainArticleTopList);
                                    }

                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mainArticleTopList != null) {
            mainArticleTopList.clear();
        }
    }
}
